﻿// -----------------------------------------------------------------------
// <copyright file="User_Manager.cs" company="Fcis">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace FaceRecognition.Models
{
    using System;
    using System.Collections.Generic;
    using System.Data.Linq;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using FaceRecognition.DataBase;
    using FaceRecognition.View;
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class User_Manager
    {
        //DataClasses1DataContext db = new DataClasses1DataContext();
        /// <summary>
        /// save info of the new user in the database
        /// if it couldn't it throw an exception "couldn't save"
        /// </summary>
        /// <param name="fname">Firstname</param>
        /// <param name="lname">Lastname</param>
        /// <param name="phone">Phone</param>
        /// <param name="email">E-mail</param>
        public void insert_user_info(string fname, string lname, string phone, string email)
        {
            //DataClasses1DataContext db = new DataClasses1DataContext();
            Users_Info user = new Users_Info();
            user.First_Name = fname;
            user.Last_Name = lname;
            user.phone = phone;
            user.email = email;
            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    db.Users_Infos.InsertOnSubmit(user);


                    db.SubmitChanges();
                    //    MessageBox.Show("You have been added Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception e)
            {
                throw e;
            }

        }
        public void update_user_info(Int32 id, string fname, string lname, string phone, string email)
        {

            //DataClasses1DataContext db1 = new DataClasses1DataContext();
            try{
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    Users_Info user = (from u in db.Users_Infos
                                       where u.ID == id
                                       select u).Single();
                    if (email != "")
                        user.email = email;
                    if (phone != "")
                        user.phone = phone;
                    if (fname != "")
                        user.First_Name = fname;
                    if (lname != "")
                        user.Last_Name = lname;
                    db.SubmitChanges();
                    //    MessageBox.Show("You have been updated Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
                } 
            }
            catch(Exception e)
            {
                throw  e;
            }


        }
        public void update_user_info_by_email( string fname, string lname, string phone, string email,string old_email)
        {
            //DataClasses1DataContext db1 = new DataClasses1DataContext();
            try
            {
            using(DataClasses1DataContext db = new DataClasses1DataContext()){

            Users_Info user = (from u in db.Users_Infos
                               where u.email == old_email
                               select u).Single();
            if (email != "")
                user.email = email;
            if (phone != "")
                user.phone = phone;
            if (fname != "")
                user.First_Name = fname;
            if (lname != "")
                user.Last_Name = lname;
           
                db.SubmitChanges();
               
               // MessageBox.Show("You have been updated Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            }
            catch(Exception e)
            {
                throw e;
            }


        }
        public bool ID_Exist(Int32 ID)
       {
        Users_Info user;

         try
                        {
                            using (DataClasses1DataContext db = new DataClasses1DataContext())
                            {
                                try
                                {
                                    user = (from u in db.Users_Infos
                                            where u.ID == ID
                                            select u).Single();
                                    return true;
                                }
                                catch
                                {
                                    return false;
                                }
                            }
                            
                        }
                        catch(Exception e)
                        {
                          //  MessageBox.Show(" the ID is not valid ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            throw e;
                        }

       }
        public bool email_exist(string email)
        {
            Users_Info user;
            
            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    try
                    {

                        user = (from u in db.Users_Infos
                                where u.email == email
                                select u).Single();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }

            }
            catch (Exception e)
            {
                //MessageBox.Show(" the e_mail is not valid ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw e;
            }

        }


        public void delete_user_info(Int32 id)
        {

            //DataClasses1DataContext db = new DataClasses1DataContext();
           try
           {
               using (DataClasses1DataContext db = new DataClasses1DataContext())
               {
                   Users_Info user = (from u in db.Users_Infos
                                      where u.ID == id
                                      select u).Single();


                   db.Users_Infos.DeleteOnSubmit(user);

                   db.SubmitChanges();
               }
                   // MessageBox.Show("You have been deleted Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public void delete_user_info_by_email(string email)
        {

            //DataClasses1DataContext db = new DataClasses1DataContext();
            try
            {
            using(DataClasses1DataContext db = new DataClasses1DataContext())
            {
            Users_Info user = (from u in db.Users_Infos
                               where u.email == email
                               select u).Single();
            db.Users_Infos.DeleteOnSubmit(user);
            
                db.SubmitChanges();
               // MessageBox.Show("You have been deleted Successfully ", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            }

            catch(Exception e)
            {
                throw e;
            }
        }

        public Int32 ID_retrival_by_email(string email)
        {
            Users_Info user;
            try
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    user = (from u in db.Users_Infos
                            where u.email == email
                            select u).Single();
                    return user.ID;
                }

            }
            catch(Exception e)
            {
                //MessageBox.Show(" the e_mail is not valid ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw e;
            }
        }
    }
}